//
//  SplashVC.m
//  CJModileAdDemo
//
//  Created by CJ on 2022/3/7.
//

#import "SplashVC.h"
#import "CustomShowView.h"
#import <AnyThinkSplash/AnyThinkSplash.h>

@interface SplashVC ()<ATSplashDelegate>

@end

@implementation SplashVC

@synthesize customView = _customView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)dealloc
{
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)loadAdData {
//    [[ATAdManager sharedManager] loadADWithPlacementID:@"b65e92607bc457" extra:@{kATSplashExtraTolerateTimeoutKey: @(3)} delegate:self];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    view.backgroundColor = [UIColor redColor];
    [[ATAdManager sharedManager] loadADWithPlacementID:@"b63f324c5bf572" extra:@{@"container_view": view} viewController:self delegate:self];
//    [[ATAdManager sharedManager] loadADWithPlacementID:@"b65e92607bc457" extra:@{@"container_view": view} delegate:self containerView:view];

}

- (void)setupUI {
    self.title = @"SplashAd";
    [self.view addSubview:self.customView];
    self.customView.frame = CGRectMake(0, 100, 200, 200);
    self.customView.center = self.view.center;
}

- (void)handleEvent:(NSInteger)eve {
    if (eve == 0) {
        NSLog(@"事件:%s", __FUNCTION__);
        [self loadAdData];
        return;
    }
    if ([[ATAdManager sharedManager] splashReadyForPlacementID:@"b63f324c5bf572"]) {
        [[ATAdManager sharedManager] showSplashWithPlacementID:@"b63f324c5bf572"
                                                             scene:@"1"
                                                             window:[UIApplication sharedApplication].keyWindow
                                                             extra:nil
                                                             delegate:self];
    }
}

- (CustomShowView *)customView {
    if (!_customView) {
        _customView = [[CustomShowView alloc] init];
        __weak SplashVC *weakSelf = self;
        _customView.event = ^(NSInteger eve) {
            [weakSelf handleEvent:eve];
        };
    }
    return _customView;
}

#pragma mark ATSplashDelegate
- (void)didFinishLoadingADWithPlacementID:(NSString *)placementID {
    [self.customView canShowStatus:true errorMsg:@""];
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)didFinishLoadingSplashADWithPlacementID:(NSString *)placementID
                                      isTimeout:(BOOL)isTimeout {
    NSLog(@"事件:%s--%@", __FUNCTION__, isTimeout ? @"已经超时" : @"没有超时");
}

- (void)didFailToLoadADWithPlacementID:(NSString *)placementID error:(NSError *)error {
    NSLog(@"事件:%s%@", __FUNCTION__, error);
}

/// Splash ad displayed successfully
- (void)splashDidShowForPlacementID:(NSString *)placementID
                              extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
}

/// Splash ad click
- (void)splashDidClickForPlacementID:(NSString *)placementID
                               extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
}



/// Splash ad closed
- (void)splashDidCloseForPlacementID:(NSString *)placementID
                               extra:(NSDictionary *)extra {
    [self.customView canShowStatus:false errorMsg:@""];
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)didFailBiddingADSourceWithPlacementID:(NSString *)placementID extra:(NSDictionary *)extra error:(NSError *)error {
    
}

@end
